/* 
 * ---------------------------------------------------------------------
 * This is  audio class for android that implementss the audio interface.
 * 2011, by Wu Mu Xian
 * --------------------------------------------------------------------- 
*/

package mygame.myandroidgameframework;

import java.io.IOException;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;

import mygame.basicgameframework.Audio;
import mygame.basicgameframework.Music;
import mygame.basicgameframework.Sound;

public class MyAndroidAudio implements Audio {
	
	AssetManager assets; // the android asset manager
	SoundPool soundPool; // the android sound pool

	// constructor
	public MyAndroidAudio(Activity activity) {
		activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
		this.assets = activity.getAssets();
		this.soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC,0);
	}
	
	// create new music
	public Music newMusic(String fileName) {
		try {
			AssetFileDescriptor assetDescriptor = assets.openFd(fileName);
			return new MyAndroidMusic(assetDescriptor);
		} catch(IOException e) {
			throw new RuntimeException("Couldn't load music '" + fileName + "'");
		}
	}

	// create new sound
	public Sound newSound(String fileName) {
		try { 
            AssetFileDescriptor assetDescriptor = assets.openFd(fileName); 
            int soundId = soundPool.load(assetDescriptor, 0); 
            return new MyAndroidSound(soundPool, soundId); 
        } catch (IOException e) { 
            throw new RuntimeException("Couldn't load sound '" + fileName + "'"); 
        }
	}

}
